package com.box.receiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.hardware.display.DisplayManager;
import android.preference.ListPreference;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/7/6.
 */
public class Hdmi extends BroadcastReceiver {
	int format = 0;
	private String HDMI = "BOX";
	private ListPreference list;
	private static SharedPreferences preferences;
	private Editor editor;
	private static Hdmi hdi;
	private String[] hdmi = new String[] { "404", "405", "408", "409", "40a",
			"41c" };
	private String BAG = "HDMILOG";
	private boolean isSupport=false;
	@Override
	public void onReceive(Context context, Intent intent) {
		preferences = context
				.getSharedPreferences("text", Context.MODE_PRIVATE);
		editor = preferences.edit();
		String names;

		String action = intent.getAction();
		if (action.equals("KEY_HDMI")) {
//			Toast.makeText(context,
//						"KEY_HDMI  ---launcher",
//						Toast.LENGTH_LONG).show();
			Log.d("zhao", "KEY_HDMI--launcher");
			preferences = context.getSharedPreferences("text",
					Activity.MODE_PRIVATE);
			Log.i(BAG, "aaaaaaaaaa");
			if (getString() != null) {
				Log.i(BAG, "aaaaaaaaaabbb");
				String name = getString();
				Log.i(BAG, name);
				for (int i = 0; i < hdmi.length; i++) {
					if (name.equals(hdmi[i])) {
						Log.i(BAG, "aaaaaaaaaabbbcc" + hdmi[i]);
						if (i + 1 > 5) {
							Log.i(BAG, "aaaaaaaaaabbbccdd" + hdmi[i]);
							names = hdmi[0];
						} else {
							Log.i(BAG, "aaaaaaaaaabbbddddd" + hdmi[i]);
							names = hdmi[i + 1];
							Log.i(BAG, "names" + hdmi[i]);
						}
//						switchDispFormat(names, false,context);

						putString(names);
					}
				}
			} else {
				putString(hdmi[0]);
				Log.i(BAG, "aaaaaaaaaabbbwwww");
//				switchDispFormat(hdmi[0], false,context);

				Log.i(BAG, "aaaaaaaaaasssssss");
				putString(hdmi[1]);
			}
		}

	}

	public void putString(String name) {
		Log.i(BAG, "aaaaaaaaaasssssssoooo");
		editor.putString("name", name);
		editor.commit();
	}

	public String getString() {
		Log.i(BAG, "aaaaaaaaaasssssssvvvvvv");
		return preferences.getString("name", null);
	}

//	public void switchDispFormat(String value, boolean save,Context context) {
//		try {
//			format = Integer.parseInt(value, 16);
//			final DisplayManager dm = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
//			int dispformat = dm.getDisplayModeFromFormat(format);
//			int mCurType = dm
//					.getDisplayOutputType(android.view.Display.TYPE_BUILT_IN);
//			isSupport = true;
//			if (isSupport) {
//				dm.setDisplayOutput(android.view.Display.TYPE_BUILT_IN, format);
//				if (save) {
//					Settings.System.putString(context.getContentResolver(),
//							Settings.System.DISPLAY_OUTPUT_FORMAT, value);
//				}
//			//	mOutputMode.setValue(value);
//			} else {
//				Toast.makeText(context,
//						//com.android.settings.R.string.display_mode_unsupport,
//						"display mode not support",
//						Toast.LENGTH_LONG).show();
//			}
//		} catch (NumberFormatException e) {
//			Log.w(BAG, "Invalid display output format!");
//		}
//	}
}
